package com.softstao.guoyu.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.KeyEvent;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.softstao.guoyu.R;
import com.softstao.guoyu.base.BaseActivity;
import com.softstao.guoyu.global.SharePreferenceManager;
import com.softstao.guoyu.global.UserManager;
import com.softstao.softstaolibrary.library.widget.ProgressWebView;

import butterknife.BindView;

/**
 * Created by vaikus on 2017/7/28.
 */

public class WebViewActivity extends BaseActivity {

    @BindView(R.id.web_view)
    ProgressWebView webView;
    private String url;
    private String title;
    private String launch;

    @Override
    public int _ContentView() {
        return R.layout.activity_web;
    }

    @Override
    public void initView() {
        title = getIntent().getStringExtra("title");
        url = getIntent().getStringExtra("url");
        launch = getIntent().getStringExtra("launch");
        initTitle(title);

        this.webView.getSettings().setJavaScriptEnabled(true);
        this.webView.addJavascriptInterface(new Object(){
            @JavascriptInterface           // For API 17+
            public void performClick(String strl) {
            }
        },"ok");
        this.webView.getSettings().setDefaultTextEncodingName("UTF-8");
        this.webView.getSettings().setAllowFileAccess(true);
        this.webView.getSettings().setLoadWithOverviewMode(true);
        this.webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        this.webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("tel://")) {
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    url = url.replace("tel://","tel:");
                    Uri data = Uri.parse(url);
                    intent.setData(data);
                    startActivity(intent);
                }else if (url.startsWith("guoyu://login")){
                    finish();
                } else {
                    view.loadUrl(url);
                }
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == event.KEYCODE_BACK) {//监听返回键，如果可以后退就后退
            if (webView.canGoBack()) {
                webView.goBack();
                return true;

            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onResume() {
        super.onResume();
        webView.loadUrl(url);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        webView.removeAllViews();
        webView.destroy();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (launch!=null && !launch.equals("") && launch.equals("1")){
            if (UserManager.getInstance().getUser()==null){
                startActivity(new Intent(context,LoginActivity.class));
            }else if (SharePreferenceManager.getInstance().getAgentId()==0){
                startActivity(new Intent(context,LoginActivity.class));
            }else {
                Intent intent = new Intent(getContext(), MainActivity.class);
                startActivity(intent);
            }
            finish();
        }
    }
}
